/*
 *  Copyright 2016 http://www.kedacomframework.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.kedacom.ctsp.authz.oauth2.entity;

import com.google.common.collect.Sets;
import com.kedacom.ctsp.web.entity.CrudEntity;
import com.kedacom.ctsp.web.entity.SortSupportBean;
import com.kedacom.ctsp.web.entity.StatefulBean;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * @author zhouhao
 */
public interface Client extends CrudEntity<String>, StatefulBean<Integer>, SortSupportBean {

    /**
     * id
     */
    String ID = "id";
    /**
     * 标识
     */
    String SIGN = "sign";
    /**
     * 负责人Id
     */
    String OWNER_ID = "ownerId";

    String getSign();

    String getName();

    void setName(String name);

    String getSecret();

    void setSecret(String secret);

    String getRedirectUri();

    void setRedirectUri(String redirectUri);

    /**
     * @return 客户端所有者
     */
    String getOwnerId();

    void setOwnerId(String ownerId);

    String getDescribe();

    void setDescribe(String describe);

    String getSupportGrantTypes();

    void setSupportGrantTypes(String supportGrantType);

    String getDefaultGrantScope();

    void setDefaultGrantScope(String defaultGrantScope);


    /**
     * @return 状态
     * @see com.kedacom.ctsp.web.service.StatusEnum
     */
    @Override
    Integer getStatus();

    /**
     * @return 客户端支持的认证类型
     * @see com.kedacom.ctsp.authz.oauth2.core.GrantType
     */

    default boolean isSupportGrantType(String grantType) {
        if (StringUtils.isNotBlank(getSupportGrantTypes())) {
            Set<String> supports = Sets.newHashSet(getSupportGrantTypes().split(","));
            return supports != null && (supports.contains(grantType) || supports.contains("*"));
        }
        return false;

    }

    /**
     * 客户端支持的权限范围
     *
     * @return
     */
    default Set<String> getSupportGrantType() {
        if (StringUtils.isNotBlank(getSupportGrantTypes())) {
            Set<String> supports = Sets.newHashSet(getDefaultGrantScope().split(","));
            return supports;
        }
        return Sets.newHashSet();

    }

    /**
     * 客户端支持的权限范围
     *
     * @return
     */
    default Set<String> getSupportGrantScope() {
        if (StringUtils.isNotBlank(getDefaultGrantScope())) {
            Set<String> supports = Sets.newHashSet(getDefaultGrantScope().split(","));
            return supports;
        }
        return Sets.newHashSet();

    }
}
